package com.aaie.gru.user.controller;


import com.aaie.gru.gruenum.UserType;
import com.aaie.gru.login.entity.LoginUser;
import com.aaie.gru.picture.entity.GruSysPicture;
import com.aaie.gru.picture.service.IGruSysPictureService;
import com.aaie.gru.response.ResMessage;
import com.aaie.gru.theme.entity.GruSysThemeConfig;
import com.aaie.gru.theme.service.IGruSysThemeConfigService;
import com.aaie.gru.user.entity.*;
import com.aaie.gru.user.service.IGruSysUserService;
import com.aaie.gru.util.CommonUtil;
import com.aaie.gru.util.ConstantUtil;
import com.aaie.gru.util.ExceptionEnums;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author lvxing
 * @since 2019-08-10
 */
@Api(tags = "用户类型(0:员工,1:监护人,2:家长,3:学校,4:学生)模块")
@RestController
@RequestMapping("/user")
public class GruSysUserController {

    private static final Logger logger = LoggerFactory.getLogger(GruSysUserController.class);

    @Autowired
    IGruSysUserService gruSysUserService;

    @Autowired
    IGruSysPictureService gruSysPictureService;

    @Autowired
    IGruSysThemeConfigService iGruSysThemeConfigService;

    // ==============================================监护人部分开始 监护人部分包括学校id集合
    @ApiOperation(value = "获取所有监护人列表", notes = "获取所有监护人列表")
    @GetMapping("/getAllGuardianList")
    public String getAllGuardianList() {
        QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", UserType.GUARDIAN.getValue());
        // 未删除状态
        queryWrapper.eq("del_flag", "0");
        // 按更新时间倒叙排序
        queryWrapper.orderByDesc("update_time_sort");
        List<GruSysUser> gruSysUserList = gruSysUserService.list(queryWrapper);
        List<GruSysGuardian> gruSysGuardianList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(gruSysUserList)) {
            for (GruSysUser gruSysUser : gruSysUserList) {
                GruSysGuardian gruSysGuardian = new GruSysGuardian();
                BeanUtils.copyProperties(gruSysUser, gruSysGuardian);
                gruSysGuardianList.add(gruSysGuardian);
            }
        }
        return ResMessage.ok(gruSysGuardianList);
    }

    /**
     * 按条件获取监护人信息，由于只是个查询，而且不需要写service，所以在controller中写代码就可以了
     * 如果后期有需要，也可以写到service
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "分页有查询条件获取监护人列表", notes = "分页有查询条件获取监护人列表")
    @PostMapping("/getGuardianList")
    public String getGuardianList(@RequestBody GruSysGuardianDto param) {
        IPage<GruSysUser> page = null;
        List<GruSysGuardian> gruSysGuardianList = null;
        try {
            // 定义分页
            Page<GruSysUser> gruSysUserPage = new Page<>(param.getCurrent(), param.getSize());
            // 查询条件
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            // 姓名 模糊查询
            if (StringUtils.isNotEmpty(param.getSearch())) {
                queryWrapper.and(qw -> qw.like("user_name", param.getSearch())
                        .or().like("user_account", param.getSearch()));
            }
            // 类型：监护人
            queryWrapper.eq("user_type", UserType.GUARDIAN.getValue());
            // 未删除状态
            queryWrapper.eq("del_flag", "0");
            // 按更新时间倒叙排序
            queryWrapper.orderByDesc("update_time_sort");
            // 查询结果
            page = gruSysUserService.page(gruSysUserPage, queryWrapper);
            // 取出记录
            List<GruSysUser> records = page.getRecords();
            // user全属性转换成监护人 records -> gruSysGuardianList
            gruSysGuardianList = new ArrayList<>();
            for (GruSysUser gruSysUser : records) {
                GruSysGuardian gruSysGuardian = new GruSysGuardian();
                BeanUtils.copyProperties(gruSysUser, gruSysGuardian);
                gruSysGuardianList.add(gruSysGuardian);
            }
        } catch (BeansException e) {
            e.printStackTrace();
            return ResMessage.error("/user/getGuardianList报错", 500);
        }
        return ResMessage.ok(gruSysGuardianList, param.getCurrent(), param.getSize(), page.getTotal());
    }

    @ApiOperation(value = "根据家长学生ID查询监护人", notes = "根据家长学生ID查询监护人")
    @PostMapping("/getGurdianInfoByUserId")
    public String getGurdianInfoByUserId(@RequestBody GruSysStuDto gruSysStuDto,HttpSession session){
        IPage<GruSysUser> page = null;
        try{
            String userIdStr = CommonUtil.getUser(session).getUserId();
            String userTypeStr = CommonUtil.getUser(session).getUserType();
            if(userTypeStr.equals(UserType.STUDENT.getValue())){
                gruSysStuDto.setUserId(userIdStr);
            }else if(userTypeStr.equals(UserType.PARENT.getValue())){
                gruSysStuDto.setStuFatherId(userIdStr);
                //gruSysStuDto.setStuMotherId(userTypeStr);
            }
            gruSysStuDto.setCurrentUserId(userIdStr);
            page = gruSysUserService.getGruSysGurdianList(gruSysStuDto);
        }catch (Exception e){
            //e.printStackTrace();
            return ResMessage.error("/user/getGurdianInfoByUserId", 500);
        }

        return ResMessage.ok(page.getRecords(), gruSysStuDto.getCurrent(), gruSysStuDto.getSize(), page.getTotal());
    }

    /**
     * 添加监护人数据
     *
     * @param gruSysGuardian
     * @return
     */
    @ApiOperation(value = "添加监护人数据", notes = "添加监护人数据")
    @PostMapping("/addGuardian")
    public String addGuardian(@RequestBody GruSysGuardian gruSysGuardian, HttpSession session) {
        GruSysGuardian result = null;
        try {
            if (StringUtils.isNotEmpty(gruSysGuardian.getUserId())) {
                return ResMessage.error("新增的数据不能有主键id", 200);
            }
            // 账号不能为空
            String userAccount = gruSysGuardian.getUserAccount();
            if (StringUtils.isEmpty(userAccount)) {
                return ResMessage.error("账号不能为空", 200);
            }
            // 密码不能为空
            if (StringUtils.isEmpty(gruSysGuardian.getUserPassword())) {
                return ResMessage.error("密码不能为空", 200);
            }
            // 账号不能重复
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", userAccount);
            queryWrapper.eq("del_flag", "0");
            List<GruSysUser> list = gruSysUserService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                return ResMessage.error("账号不能重复", 200);
            }
            gruSysGuardian.setUserType(UserType.GUARDIAN.getValue());
            String updateTime = CommonUtil.generateLocalDateTime();
            long updateTimeSort = CommonUtil.generateCurrentTimeMillis();
            gruSysGuardian.setCreateName(CommonUtil.getUser(session).getUserName());
            gruSysGuardian.setCreateTime(updateTime);
            gruSysGuardian.setCreateTimeSort(updateTimeSort);
            gruSysGuardian.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysGuardian.setUpdateTime(updateTime);
            gruSysGuardian.setUpdateTimeSort(updateTimeSort);
            result = gruSysUserService.insertGruSysGuardian(gruSysGuardian);
            if (result == null) {
                return ResMessage.error("数据插入失败", 200);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/addGuardian报错", 500);
        }
        return ResMessage.ok(result);
    }

    /**
     * 更新监护人数据
     *
     * @param gruSysGuardian
     * @return
     */
    @ApiOperation(value = "更新监护人数据", notes = "更新监护人数据")
    @PostMapping("/editGuardian")
    public String editGuardian(@RequestBody GruSysGuardian gruSysGuardian, HttpSession session) {
        GruSysGuardian result = null;
        try {
            if (StringUtils.isEmpty(gruSysGuardian.getUserId())) {
                return ResMessage.error("更新的数据必须有主键id", 200);
            }
            // 账号不能为空
            String userAccount = gruSysGuardian.getUserAccount();
            if (StringUtils.isEmpty(userAccount)) {
                return ResMessage.error("账号不能为空", 200);
            }
            // 账号不能重复
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", userAccount);
            queryWrapper.eq("del_flag", "0");
            queryWrapper.ne("user_id", gruSysGuardian.getUserId());
            List<GruSysUser> list = gruSysUserService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                return ResMessage.error("账号不能重复", 200);
            }
            gruSysGuardian.setUserType(UserType.GUARDIAN.getValue());
            // 创建人只能取用户账号，不取用户名称了，因为用户名称不是同一个字段，取起来太麻烦
            gruSysGuardian.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysGuardian.setUpdateTime(CommonUtil.generateLocalDateTime());
            gruSysGuardian.setUpdateTimeSort(CommonUtil.generateCurrentTimeMillis());
            result = gruSysUserService.updateGruSysGuardian(gruSysGuardian);
            if (result == null) {
                return ResMessage.error("数据更新失败", 200);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/editGuardian报错", 500);
        }
        return ResMessage.ok(result);
    }

    @ApiOperation(value = "按id监护人数据", notes = "按id监护人数据")
    @GetMapping("/getGuardian")
    public String getGuardian(String userId) {
        GruSysGuardian gruSysGuardian = null;
        try {
            if (StringUtils.isEmpty(userId)) {
                return ResMessage.error("获取数据必须有主键id", 200);
            }
            GruSysUser gruSysUser = gruSysUserService.getById(userId);
            gruSysGuardian = new GruSysGuardian();
            BeanUtils.copyProperties(gruSysUser, gruSysGuardian);
            if (gruSysGuardian == null) {
                return ResMessage.error("获取失败", 200);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/getGuardian报错", 500);
        }
        return ResMessage.ok(gruSysGuardian);
    }

    @ApiOperation(value = "删除监护人数据", notes = "删除监护人数据")
    @PostMapping("/delGuardian")
    public String delGuardian(@RequestBody GruSysGuardian gruSysGuardian) {
        try {
            if (StringUtils.isEmpty(gruSysGuardian.getUserId())) {
                return ResMessage.error("删除数据必须有主键id", 200);
            }
            gruSysGuardian.setDelFlag("1");
            GruSysUser gruSysUser = new GruSysUser();
            BeanUtils.copyProperties(gruSysGuardian, gruSysUser);
            // 逻辑删除
            boolean b = gruSysUserService.updateById(gruSysUser);
            if (b) {
                return ResMessage.ok("删除数据成功");
            }
            return ResMessage.error("删除数据失败", 200);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/delGuardian报错", 500);
        }

    }

    // ==============================================监护人部分结束

    // ==============================================家长部分开始 家长部分也是单表

    @ApiOperation(value = "获取所有家长数据", notes = "获取所有家长数据")
    @GetMapping("/getAllParentList")
    public String getAllParentList() {
        QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", UserType.PARENT.getValue());
        // 未删除状态
        queryWrapper.eq("del_flag", "0");
        // 按更新时间倒叙排序
        queryWrapper.orderByDesc("update_time_sort");
        List<GruSysUser> gruSysUserList = gruSysUserService.list(queryWrapper);
        List<GruSysParent> gruSysParentList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(gruSysUserList)) {
            for (GruSysUser gruSysUser : gruSysUserList) {
                GruSysParent gruSysParent = new GruSysParent();
                BeanUtils.copyProperties(gruSysUser, gruSysParent);
                gruSysParentList.add(gruSysParent);
            }
        }
        return ResMessage.ok(gruSysParentList);
    }

    @ApiOperation(value = "获取所有父亲数据", notes = "获取所有父亲数据")
    @GetMapping("/getAllFatherList")
    public String getAllFatherList() {
        QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", UserType.PARENT.getValue());
        queryWrapper.eq("parent_role", "父亲");
        // 未删除状态
        queryWrapper.eq("del_flag", "0");
        // 按更新时间倒叙排序
        queryWrapper.orderByDesc("update_time_sort");
        List<GruSysUser> gruSysUserList = gruSysUserService.list(queryWrapper);
        List<GruSysParent> gruSysParentList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(gruSysUserList)) {
            for (GruSysUser gruSysUser : gruSysUserList) {
                GruSysParent gruSysParent = new GruSysParent();
                BeanUtils.copyProperties(gruSysUser, gruSysParent);
                gruSysParentList.add(gruSysParent);
            }
        }
        return ResMessage.ok(gruSysParentList);
    }

    @ApiOperation(value = "获取所有母亲数据", notes = "获取所有母亲数据")
    @GetMapping("/getAllMotherList")
    public String getAllMotherList() {
        QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", UserType.PARENT.getValue());
        queryWrapper.eq("parent_role", "母亲");
        // 未删除状态
        queryWrapper.eq("del_flag", "0");
        // 按更新时间倒叙排序
        queryWrapper.orderByDesc("update_time_sort");
        List<GruSysUser> gruSysUserList = gruSysUserService.list(queryWrapper);
        List<GruSysParent> gruSysParentList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(gruSysUserList)) {
            for (GruSysUser gruSysUser : gruSysUserList) {
                GruSysParent gruSysParent = new GruSysParent();
                BeanUtils.copyProperties(gruSysUser, gruSysParent);
                gruSysParentList.add(gruSysParent);
            }
        }
        return ResMessage.ok(gruSysParentList);
    }

    /**
     * 获取家长列表
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "分页获取家长列表", notes = "分页获取家长列表")
    @PostMapping("/getParentList")
    public String getParentList(@RequestBody GruSysParentDto param) {
        IPage<GruSysUser> page = null;
        List<GruSysParent> gruSysParentList = null;
        try {
            // 定义分页
            Page<GruSysUser> gruSysUserPage = new Page<>(param.getCurrent(), param.getSize());
            // 查询条件
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            // 姓名 模糊查询
            if (StringUtils.isNotEmpty(param.getSearch())) {
                queryWrapper.and(qw -> qw.like("user_name", param.getSearch())
                        .or().like("user_account", param.getSearch()));
            }
            // 类型：监护人
            queryWrapper.eq("user_type", UserType.PARENT.getValue());
            // 未删除状态
            queryWrapper.eq("del_flag", "0");
            // 按更新时间倒叙排序
            queryWrapper.orderByDesc("update_time_sort");
            // 查询结果
            page = gruSysUserService.page(gruSysUserPage, queryWrapper);
            // 取出记录
            List<GruSysUser> records = page.getRecords();
            // user全属性转换成家长 records -> gruSysParentList
            gruSysParentList = new ArrayList<>();
            for (GruSysUser gruSysUser : records) {
                GruSysParent gruSysParent = new GruSysParent();
                BeanUtils.copyProperties(gruSysUser, gruSysParent);
                gruSysParentList.add(gruSysParent);
            }
        } catch (BeansException e) {
            e.printStackTrace();
            return ResMessage.error("/user/getParentList报错", 500);
        }
        return ResMessage.ok(gruSysParentList, param.getCurrent(), param.getSize(), page.getTotal());
    }

    /**
     * 添加家长数据
     *
     * @param gruSysParent
     * @return
     */
    @ApiOperation(value = "添加家长数据", notes = "添加家长数据")
    @PostMapping("/addParent")
    public String addParent(@RequestBody GruSysParent gruSysParent, HttpSession session) {
        GruSysParent result = null;
        try {
            if (StringUtils.isNotEmpty(gruSysParent.getUserId())) {
                return ResMessage.error("新增的数据不能有主键id", 200);
            }
            // 账号不能为空
            String userAccount = gruSysParent.getUserAccount();
            if (StringUtils.isEmpty(userAccount)) {
                return ResMessage.error("账号不能为空", 200);
            }
            // 密码不能为空
            if (StringUtils.isEmpty(gruSysParent.getUserPassword())) {
                return ResMessage.error("密码不能为空", 200);
            }
            // 账号不能重复
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", userAccount);
            queryWrapper.eq("del_flag", "0");
            List<GruSysUser> list = gruSysUserService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                return ResMessage.error("账号不能重复", 200);
            }
            gruSysParent.setUserType(UserType.PARENT.getValue());
            String updateTime = CommonUtil.generateLocalDateTime();
            long updateTimeSort = CommonUtil.generateCurrentTimeMillis();
            gruSysParent.setCreateName(CommonUtil.getUser(session).getUserName());
            gruSysParent.setCreateTime(updateTime);
            gruSysParent.setCreateTimeSort(updateTimeSort);
            gruSysParent.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysParent.setUpdateTime(updateTime);
            gruSysParent.setUpdateTimeSort(updateTimeSort);
            result = gruSysUserService.insertGruSysParent(gruSysParent);
            if (result == null) {
                return ResMessage.error("数据插入失败", 200);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/addParent报错", 500);
        }
        return ResMessage.ok(result);
    }

    /**
     * 更新家长数据
     *
     * @param gruSysParent
     * @return
     */
    @ApiOperation(value = "更新家长数据", notes = "更新家长数据")
    @PostMapping("/editParent")
    public String editParent(@RequestBody GruSysParent gruSysParent, HttpSession session) {
        GruSysParent result = null;
        try {
            if (StringUtils.isEmpty(gruSysParent.getUserId())) {
                return ResMessage.error("更新的数据必须有主键id", 200);
            }
            // 账号不能为空
            String userAccount = gruSysParent.getUserAccount();
            if (StringUtils.isEmpty(userAccount)) {
                return ResMessage.error("账号不能为空", 200);
            }
            // 账号不能重复
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", userAccount);
            queryWrapper.eq("del_flag", "0");
            queryWrapper.ne("user_id", gruSysParent.getUserId());
            List<GruSysUser> list = gruSysUserService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                return ResMessage.error("账号不能重复", 200);
            }
            gruSysParent.setUserType(UserType.PARENT.getValue());
            // 创建人只能取用户账号，不取用户名称了，因为用户名称不是同一个字段，取起来太麻烦
            gruSysParent.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysParent.setUpdateTime(CommonUtil.generateLocalDateTime());
            gruSysParent.setUpdateTimeSort(CommonUtil.generateCurrentTimeMillis());
            result = gruSysUserService.updateGruSysParent(gruSysParent);
            if (result == null) {
                return ResMessage.error("数据更新失败", 200);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/editParent报错", 500);
        }
        return ResMessage.ok(result);
    }

    @ApiOperation(value = "按id获取家长数据", notes = "按id获取家长数据")
    @GetMapping("/getParent")
    public String getParent(String userId) {
        GruSysParent gruSysParent = null;
        try {
            if (StringUtils.isEmpty(userId)) {
                return ResMessage.error("获取数据必须有主键id", 200);
            }
            GruSysUser gruSysUser = gruSysUserService.getById(userId);
            gruSysParent = new GruSysParent();
            BeanUtils.copyProperties(gruSysUser, gruSysParent);
            if (gruSysParent == null) {
                return ResMessage.error("获取失败", 200);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/getParent报错", 500);
        }
        return ResMessage.ok(gruSysParent);
    }

    @ApiOperation(value = "删除家长数据", notes = "删除家长数据")
    @PostMapping("/delParent")
    public String delParent(@RequestBody GruSysParent gruSysParent) {
        try {
            if (StringUtils.isEmpty(gruSysParent.getUserId())) {
                return ResMessage.error("删除数据必须有主键id", 200);
            }
            gruSysParent.setDelFlag("1");
            GruSysUser gruSysUser = new GruSysUser();
            BeanUtils.copyProperties(gruSysParent, gruSysUser);
            // 逻辑删除
            boolean b = gruSysUserService.updateById(gruSysUser);
            if (b) {
                return ResMessage.ok("删除数据成功");
            }
            return ResMessage.error("删除数据失败", 200);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/delParent报错", 500);
        }

    }

    // ==============================================家长部分结束

    // ==============================================学校部分开始 学校部分是单表

    @ApiOperation(value = "获取所有学校数据列表", notes = "获取所有学校数据列表")
    @GetMapping("/getAllSchoolList")
    public String getAllSchoolList() {
        QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", UserType.SCHOOL.getValue());
        // 未删除状态
        queryWrapper.eq("del_flag", "0");
        // 按更新时间倒叙排序
        queryWrapper.orderByDesc("update_time_sort");
        List<GruSysUser> gruSysUserList = gruSysUserService.list(queryWrapper);
        List<GruSysSchool> gruSysSchoolList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(gruSysUserList)) {
            for (GruSysUser gruSysUser : gruSysUserList) {
                GruSysSchool gruSysSchool = new GruSysSchool();
                BeanUtils.copyProperties(gruSysUser, gruSysSchool);
                gruSysSchoolList.add(gruSysSchool);
            }
        }
        return ResMessage.ok(gruSysSchoolList);
    }

    /**
     * 按条件获取学校信息
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "分页获取学校数据列表", notes = "分页获取学校数据列表")
    @PostMapping("/getSchoolList")
    public String getSchoolList(@RequestBody GruSysSchoolDto param) {
        IPage<GruSysUser> page = null;
        List<GruSysSchool> gruSysSchoolList = null;
        try {
            // 定义分页
            Page<GruSysUser> gruSysUserPage = new Page<>(param.getCurrent(), param.getSize());
            // 查询条件
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(param.getSearch())) {
                queryWrapper.and(qw -> qw.like("user_name", param.getSearch())
                        .or().like("user_account", param.getSearch()));
            }

            //高级搜索查询条件
            //学校地址
            if (StringUtils.isNotEmpty(param.getSchoolAddress())){
                queryWrapper.eq("school_address", param.getSchoolAddress());
            }

            //学校名称
            if (StringUtils.isNotEmpty(param.getUserName())){
                queryWrapper.eq("user_name", param.getUserName());
            }

            //学校账号
            if (StringUtils.isNotEmpty(param.getUserAccount())){
                queryWrapper.eq("user_account", param.getUserAccount());
            }

            //联系老师
            if (StringUtils.isNotEmpty(param.getSchoolTeacher())){
                queryWrapper.eq("school_teacher", param.getSchoolTeacher());
            }

            //联系电话
            if (StringUtils.isNotEmpty(param.getSchoolPhone())){
                queryWrapper.eq("school_phone", param.getSchoolPhone());
            }

            //联系邮箱
            if (StringUtils.isNotEmpty(param.getSchoolEmail())){
                queryWrapper.eq("school_email", param.getSchoolEmail());
            }

            // 类型：监护人
            queryWrapper.eq("user_type", UserType.SCHOOL.getValue());
            // 未删除状态
            queryWrapper.eq("del_flag", "0");
            // 按更新时间倒叙排序
            queryWrapper.orderByDesc("update_time_sort");
            // 查询结果
            page = gruSysUserService.page(gruSysUserPage, queryWrapper);
            // 取出记录
            List<GruSysUser> records = page.getRecords();
            // user全属性转换成监护人 records -> gruSysGuardianList

            gruSysSchoolList = new ArrayList<>();
            for (GruSysUser gruSysUser : records) {
                GruSysSchool gruSysSchool = new GruSysSchool();
                BeanUtils.copyProperties(gruSysUser, gruSysSchool);
                gruSysSchoolList.add(gruSysSchool);
            }
        } catch (BeansException e) {
            e.printStackTrace();
            return ResMessage.error("/user/getSchoolList报错", 500);
        }
        return ResMessage.ok(gruSysSchoolList, param.getCurrent(), param.getSize(), page.getTotal());
    }

    /**
     * 添加学校数据
     *
     * @param gruSysSchool
     * @return
     */
    @ApiOperation(value = "添加学校数据", notes = "添加学校数据")
    @PostMapping("/addSchool")
    public String addSchool(@RequestBody GruSysSchool gruSysSchool, HttpSession session) {
        GruSysSchool result = null;
        try {
            if (StringUtils.isNotEmpty(gruSysSchool.getUserId())) {
                return ResMessage.error("新增的数据不能有主键id", 200);
            }
            // 账号不能为空
            String userAccount = gruSysSchool.getUserAccount();
            if (StringUtils.isEmpty(userAccount)) {
                return ResMessage.error("账号不能为空", 200);
            }
            // 密码不能为空
            if (StringUtils.isEmpty(gruSysSchool.getUserPassword())) {
                return ResMessage.error("密码不能为空", 200);
            }
            // 账号不能重复
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", userAccount);
            queryWrapper.eq("del_flag", "0");
            List<GruSysUser> list = gruSysUserService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                return ResMessage.error("账号不能重复", 200);
            }
            gruSysSchool.setUserType(UserType.SCHOOL.getValue());
            String updateTime = CommonUtil.generateLocalDateTime();
            long updateTimeSort = CommonUtil.generateCurrentTimeMillis();
            gruSysSchool.setCreateName(CommonUtil.getUser(session).getUserName());
            gruSysSchool.setCreateTime(updateTime);
            gruSysSchool.setCreateTimeSort(updateTimeSort);
            gruSysSchool.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysSchool.setUpdateTime(updateTime);
            gruSysSchool.setUpdateTimeSort(updateTimeSort);
            result = gruSysUserService.insertGruSysSchool(gruSysSchool);
            if (result == null) {
                return ResMessage.error("数据插入失败", 200);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/addSchool报错", 500);
        }
        return ResMessage.ok(result);
    }

    /**
     * 更新学校数据
     *
     * @param gruSysSchool
     * @return
     */
    @ApiOperation(value = "更新学校数据", notes = "更新学校数据")
    @PostMapping("/editSchool")
    public String editSchool(@RequestBody GruSysSchool gruSysSchool, HttpSession session) {
        GruSysSchool result = null;
        try {
            if (StringUtils.isEmpty(gruSysSchool.getUserId())) {
                return ResMessage.error("更新的数据必须有主键id", 200);
            }
            // 账号不能为空
            String userAccount = gruSysSchool.getUserAccount();
            if (StringUtils.isEmpty(userAccount)) {
                return ResMessage.error("账号不能为空", 200);
            }
            // 账号不能重复
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", userAccount);
            queryWrapper.eq("del_flag", "0");
            queryWrapper.ne("user_id", gruSysSchool.getUserId());
            List<GruSysUser> list = gruSysUserService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                return ResMessage.error("账号不能重复", 200);
            }
            gruSysSchool.setUserType(UserType.SCHOOL.getValue());
            // 创建人只能取用户账号，不取用户名称了，因为用户名称不是同一个字段，取起来太麻烦
            gruSysSchool.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysSchool.setUpdateTime(CommonUtil.generateLocalDateTime());
            gruSysSchool.setUpdateTimeSort(CommonUtil.generateCurrentTimeMillis());
            result = gruSysUserService.updateGruSysSchool(gruSysSchool);
            if (result == null) {
                return ResMessage.error("数据更新失败", 200);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/editSchool报错", 500);
        }
        return ResMessage.ok(result);
    }

    @ApiOperation(value = "按id获取学校数据", notes = "按id获取学校数据")
    @GetMapping("/getSchool")
    public String getSchool(String userId) {
        GruSysSchool gruSysSchool = null;
        try {
            if (StringUtils.isEmpty(userId)) {
                return ResMessage.error("获取数据必须有主键id", 200);
            }
            GruSysUser gruSysUser = gruSysUserService.getById(userId);
            gruSysSchool = new GruSysSchool();
            BeanUtils.copyProperties(gruSysUser, gruSysSchool);
            if (gruSysSchool == null) {
                return ResMessage.error("获取失败", 200);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/getSchool报错", 500);
        }
        return ResMessage.ok(gruSysSchool);
    }

    @ApiOperation(value = "删除学校数据", notes = "删除学校数据")
    @PostMapping("/delSchool")
    public String delSchool(@RequestBody GruSysStaff gruSysStaff) {
        try {
            if (StringUtils.isEmpty(gruSysStaff.getUserId())) {
                return ResMessage.error("删除数据必须有主键id", 200);
            }
            gruSysStaff.setDelFlag("1");
            GruSysUser gruSysUser = new GruSysUser();
            BeanUtils.copyProperties(gruSysStaff, gruSysUser);
            // 逻辑删除
            boolean b = gruSysUserService.updateById(gruSysUser);
            if (b) {
                return ResMessage.ok("删除数据成功");
            }
            return ResMessage.error("删除数据失败", 200);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/delSchool报错", 500);
        }

    }

    // ==============================================学校部分结束

    // ==============================================员工部分开始 员工部分是单表

    @ApiOperation(value = "获取所有员工数据", notes = "获取所有员工数据")
    @GetMapping("/getAllStaffList")
    public String getAllStaffList() {
        QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", UserType.STAFF.getValue());
        // 未删除状态
        queryWrapper.eq("del_flag", "0");
        // 按更新时间倒叙排序
        queryWrapper.orderByDesc("update_time_sort");
        List<GruSysUser> gruSysUserList = gruSysUserService.list(queryWrapper);
        List<GruSysStaff> gruSysStaffList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(gruSysUserList)) {
            for (GruSysUser gruSysUser : gruSysUserList) {
                GruSysStaff gruSysStaff = new GruSysStaff();
                BeanUtils.copyProperties(gruSysUser, gruSysStaff);
                gruSysStaffList.add(gruSysStaff);
            }
        }
        return ResMessage.ok(gruSysStaffList);
    }

    /**
     * 按条件获取员工信息
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "分页获取员工数据", notes = "分页获取员工数据")
    @PostMapping("/getStaffList")
    public String getStaffList(@RequestBody GruSysStaffDto param) {
        IPage<GruSysUser> page = null;
        List<GruSysStaff> gruSysStaffList = null;
        try {
            // 定义分页
            Page<GruSysUser> gruSysUserPage = new Page<>(param.getCurrent(), param.getSize());
            // 查询条件
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(param.getSearch())) {
                queryWrapper.and(qw -> qw.like("user_name", param.getSearch())
                        .or().like("user_account", param.getSearch()));
            }
            // 类型：监护人
            queryWrapper.eq("user_type", UserType.STAFF.getValue());
            // 未删除状态
            queryWrapper.eq("del_flag", "0");
            // 按更新时间倒叙排序
            queryWrapper.orderByDesc("update_time_sort");
            // 查询结果
            page = gruSysUserService.page(gruSysUserPage, queryWrapper);
            // 取出记录
            List<GruSysUser> records = page.getRecords();
            // user全属性转换成监护人 records -> gruSysGuardianList
            gruSysStaffList = new ArrayList<>();
            for (GruSysUser gruSysUser : records) {
                GruSysStaff gruSysStaff = new GruSysStaff();
                BeanUtils.copyProperties(gruSysUser, gruSysStaff);
                gruSysStaffList.add(gruSysStaff);
            }
        } catch (BeansException e) {
            logger.error("=========>>>>>>>>>>>/user/getStaffList 入参:" + JSONObject.toJSONString(param));
            return ResMessage.error(ExceptionEnums.SYSTEM_ERROR.getEmsg(), ExceptionEnums.SYSTEM_ERROR.getEcode());
        }
        return ResMessage.ok(gruSysStaffList, param.getCurrent(), param.getSize(), page.getTotal());
    }

    /**
     * 添加员工数据
     *
     * @param gruSysStaff
     * @return
     */
    @ApiOperation(value = "添加员工数据", notes = "添加员工数据")
    @PostMapping("/addStaff")
    public String addStaff(@RequestBody GruSysStaff gruSysStaff, HttpSession session) {
        GruSysStaff result = null;
        try {
            if (StringUtils.isNotEmpty(gruSysStaff.getUserId())) {
                return ResMessage.error(ExceptionEnums.ADD_DATA_NO_PRI.getEmsg(), ExceptionEnums.ADD_DATA_NO_PRI.getEcode());
            }
            // 账号不能为空
            String userAccount = gruSysStaff.getUserAccount();
            if (StringUtils.isEmpty(userAccount)) {
                return ResMessage.error(ExceptionEnums.ACCOUNT_NO_EMPTY.getEmsg(), ExceptionEnums.ACCOUNT_NO_EMPTY.getEcode());
            }
            // 密码不能为空
            if (StringUtils.isEmpty(gruSysStaff.getUserPassword())) {
                return ResMessage.error(ExceptionEnums.PASSWORD_NO_EMPTY.getEmsg(), ExceptionEnums.PASSWORD_NO_EMPTY.getEcode());
            }
            // 账号不能重复
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", userAccount);
            queryWrapper.eq("del_flag", "0");
            List<GruSysUser> list = gruSysUserService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                return ResMessage.error(ExceptionEnums.ACCOUNT_NO_DUPLICATE.getEmsg(), ExceptionEnums.ACCOUNT_NO_DUPLICATE.getEcode());
            }
            gruSysStaff.setUserType(UserType.STAFF.getValue());
            String updateTime = CommonUtil.generateLocalDateTime();
            long updateTimeSort = CommonUtil.generateCurrentTimeMillis();
            LoginUser loginUser = CommonUtil.getUser(session);
            if (loginUser == null) {
                return ResMessage.error(ExceptionEnums.LOGINUSER_IS_NULL.getEmsg(), ExceptionEnums.LOGINUSER_IS_NULL.getEcode());
            } else {
                gruSysStaff.setCreateName(CommonUtil.getUser(session).getUserName());
                gruSysStaff.setCreateTime(updateTime);
                gruSysStaff.setCreateTimeSort(updateTimeSort);
                gruSysStaff.setUpdateName(CommonUtil.getUser(session).getUserName());
                gruSysStaff.setUpdateTime(updateTime);
                gruSysStaff.setUpdateTimeSort(updateTimeSort);
            }

            result = gruSysUserService.insertGruSysStaff(gruSysStaff);
            if (result == null) {
                return ResMessage.error(ExceptionEnums.DATA_INSERT_ERROR.getEmsg(), ExceptionEnums.DATA_INSERT_ERROR.getEcode());
            }
        } catch (Exception e) {
            logger.error("=========>>>>>>>>>>>/user/addStaff:" + JSONObject.toJSONString(gruSysStaff));
            return ResMessage.error(ExceptionEnums.SYSTEM_ERROR.getEmsg(), ExceptionEnums.SYSTEM_ERROR.getEcode());
        }
        return ResMessage.ok(result);
    }

    /**
     * 更新学校数据
     *
     * @param gruSysStaff
     * @return
     */
    @ApiOperation(value = "更新员工数据", notes = "更新员工数据")
    @PostMapping("/editStaff")
    public String editStaff(@RequestBody GruSysStaff gruSysStaff, HttpSession session) {
        GruSysStaff result = null;
        try {
            if (StringUtils.isEmpty(gruSysStaff.getUserId())) {
                return ResMessage.error(ExceptionEnums.ADD_DATA_NEED_PRI.getEmsg(), ExceptionEnums.ADD_DATA_NEED_PRI.getEcode());
            }
            // 账号不能为空
            String userAccount = gruSysStaff.getUserAccount();
            if (StringUtils.isEmpty(userAccount)) {
                return ResMessage.error(ExceptionEnums.ACCOUNT_NO_EMPTY.getEmsg(), ExceptionEnums.ACCOUNT_NO_EMPTY.getEcode());
            }
            // 账号不能重复
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", userAccount);
            queryWrapper.eq("del_flag", "0");
            queryWrapper.ne("user_id", gruSysStaff.getUserId());
            List<GruSysUser> list = gruSysUserService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                return ResMessage.error(ExceptionEnums.ACCOUNT_NO_DUPLICATE.getEmsg(), ExceptionEnums.ACCOUNT_NO_DUPLICATE.getEcode());
            }
            gruSysStaff.setUserType(UserType.STAFF.getValue());
            // 创建人只能取用户账号，不取用户名称了，因为用户名称不是同一个字段，取起来太麻烦
            gruSysStaff.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysStaff.setUpdateTime(CommonUtil.generateLocalDateTime());
            gruSysStaff.setUpdateTimeSort(CommonUtil.generateCurrentTimeMillis());
            result = gruSysUserService.updateGruSysStaff(gruSysStaff);
            if (result == null) {
                logger.info("=========>>>>>>>>>>>/user/delStaff:" + JSONObject.toJSONString(gruSysStaff));
                return ResMessage.error(ExceptionEnums.DATA_UPDATE_ERROR.getEmsg(), ExceptionEnums.DATA_UPDATE_ERROR.getEcode());
            }
        } catch (Exception e) {
            logger.error("=========>>>>>>>>>>>/user/editStaff:" + JSONObject.toJSONString(gruSysStaff));
            return ResMessage.error(ExceptionEnums.SYSTEM_ERROR.getEmsg(), ExceptionEnums.SYSTEM_ERROR.getEcode());
        }
        return ResMessage.ok(result);
    }

    @ApiOperation(value = "按id获取员工数据", notes = "按id获取员工数据")
    @GetMapping("/getStaff")
    public String getStaff(String userId) {
        GruSysStaff gruSysStaff = null;
        try {
            if (StringUtils.isEmpty(userId)) {
                return ResMessage.error(ExceptionEnums.ADD_DATA_NEED_PRI.getEmsg(), ExceptionEnums.ADD_DATA_NEED_PRI.getEcode());
            }
            GruSysUser gruSysUser = gruSysUserService.getById(userId);
            gruSysStaff = new GruSysStaff();
            BeanUtils.copyProperties(gruSysUser, gruSysStaff);
            if (gruSysStaff == null) {
                return ResMessage.error(ExceptionEnums.DATA_QUERY_ERROR.getEmsg(), ExceptionEnums.DATA_QUERY_ERROR.getEcode());
            }
        } catch (Exception e) {
            logger.error("=========>>>>>>>>>>>/user/getStaff userId:" + userId);
            return ResMessage.error(ExceptionEnums.SYSTEM_ERROR.getEmsg(), ExceptionEnums.SYSTEM_ERROR.getEcode());
        }
        return ResMessage.ok(gruSysStaff);
    }

    @ApiOperation(value = "删除员工数据", notes = "删除员工数据")
    @PostMapping("/delStaff")
    public String delStaff(@RequestBody GruSysStaff gruSysStaff) {
        try {
            if (StringUtils.isEmpty(gruSysStaff.getUserId())) {
                return ResMessage.error(ExceptionEnums.ADD_DATA_NEED_PRI.getEmsg(), ExceptionEnums.ADD_DATA_NEED_PRI.getEcode());
            }
            gruSysStaff.setDelFlag("1");
            GruSysUser gruSysUser = new GruSysUser();
            BeanUtils.copyProperties(gruSysStaff, gruSysUser);
            // 逻辑删除
            boolean delStaff = gruSysUserService.updateById(gruSysUser);
            if (delStaff) {
                return ResMessage.ok(ExceptionEnums.DATA_DELETE_SUCCESS.getEmsg());
            }
            return ResMessage.error(ExceptionEnums.DATA_DELETE_ERROR.getEmsg(), ExceptionEnums.DATA_DELETE_ERROR.getEcode());
        } catch (Exception e) {
            logger.error("=========>>>>>>>>>>>/user/delStaff:" + JSONObject.toJSONString(gruSysStaff));
            return ResMessage.error(ExceptionEnums.SYSTEM_ERROR.getEmsg(), ExceptionEnums.SYSTEM_ERROR.getEcode());
        }

    }

    // ==============================================员工部分结束

    // ==============================================学生部分开始 学生部分是多表 关联 监护人 父亲 母亲

    @ApiOperation(value = "获取所有学生数据", notes = "获取所有学生数据")
    @GetMapping("/getAllStuList")
    public String getAllStuList() {
        QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", UserType.STUDENT.getValue());
        // 未删除状态
        queryWrapper.eq("del_flag", "0");
        // 按更新时间倒叙排序
        queryWrapper.orderByDesc("update_time_sort");
        List<GruSysUser> gruSysUserList = gruSysUserService.list(queryWrapper);
        List<GruSysStu> gruSysStuList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(gruSysUserList)) {
            for (GruSysUser gruSysUser : gruSysUserList) {
                GruSysStu gruSysStu = new GruSysStu();
                BeanUtils.copyProperties(gruSysUser, gruSysStu);
                gruSysStuList.add(gruSysStu);
            }
        }
        return ResMessage.ok(gruSysStuList);
    }

    /**
     * 按条件获取学生信息
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "分页获取学生数据", notes = "分页获取学生数据")
    @PostMapping("/getStuList")
    public String getStuList(@RequestBody GruSysStuDto param,HttpSession session) {
        IPage<GruSysStuVo> page = null;
//        List<GruSysStuVo> gruSysStuList = null;
        try {
            /*// 定义分页
            Page<GruSysUser> gruSysUserPage = new Page<>(param.getCurrent(), param.getSize());
            // 查询条件
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            // 姓名 模糊查询
            queryWrapper.like(StringUtils.isNotEmpty(param.getUserName()), "user_name", param.getUserName());
            // 账号 精确查询
            queryWrapper.eq(StringUtils.isNotEmpty(param.getUserAccount()), "user_account", param.getUserAccount());
            // 类型：监护人
            queryWrapper.eq("user_type", UserType.STUDENT.getIndex());
            // 按更新时间倒叙排序
            queryWrapper.orderByDesc("update_time_sort");
            // 查询结果
            page = gruSysUserService.page(gruSysUserPage, queryWrapper);*/
            page = gruSysUserService.getGruSysStuList(param);
            // 取出记录
            List<GruSysStuVo> records = page.getRecords();
            // user全属性转换成监护人 records -> gruSysGuardianList
//            gruSysStuList = new ArrayList<>();
//            for (GruSysUser gruSysUser : records) {
//                GruSysStu gruSysStu = new GruSysStu();
//                BeanUtils.copyProperties(gruSysUser, gruSysStu);
//                gruSysStuList.add(gruSysStu);
//            }
            String currentUserId = CommonUtil.getUser(session).getUserId();
            for(int i = 0;i < records.size();i ++){
                GruSysStuVo gruSysStuVo = records.get(i);
                QueryWrapper<GruSysThemeConfig> queryWrapperGrySysThemeConfig = new QueryWrapper<>();
                queryWrapperGrySysThemeConfig.eq("theme_id",gruSysStuVo.getStuFatherId());
                queryWrapperGrySysThemeConfig.eq("current_user_id",currentUserId);
                GruSysThemeConfig gruSysThemeConfig = iGruSysThemeConfigService.getOne(queryWrapperGrySysThemeConfig);
                if(gruSysThemeConfig != null){
                    gruSysStuVo.setIsNewMes(gruSysThemeConfig.getThemeNew());
                }

            }

            return ResMessage.ok(records, param.getCurrent(), param.getSize(), page.getTotal());
        } catch (BeansException e) {
            e.printStackTrace();
            return ResMessage.error("/user/getStuList报错", 500);
        }
    }

    /**
     * 添加学生数据
     *
     * @param gruSysStu
     * @return
     */
    @ApiOperation(value = "添加学生数据", notes = "添加学生数据")
    @PostMapping("/addStu")
    public String addStu(@RequestBody GruSysStu gruSysStu, HttpSession session) {
        GruSysStu result = null;
        try {
            if (StringUtils.isNotEmpty(gruSysStu.getUserId())) {
                return ResMessage.error("新增的数据不能有主键id", 200);
            }
            // 账号不能为空
            String userAccount = gruSysStu.getUserAccount();
            if (StringUtils.isEmpty(userAccount)) {
                return ResMessage.error("账号不能为空", 200);
            }
            // 密码不能为空
            if (StringUtils.isEmpty(gruSysStu.getUserPassword())) {
                return ResMessage.error("密码不能为空", 200);
            }
            // 账号不能重复
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", userAccount);
            queryWrapper.eq("del_flag", "0");
            List<GruSysUser> list = gruSysUserService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                return ResMessage.error("账号不能重复", 200);
            }
            gruSysStu.setUserType(UserType.STUDENT.getValue());
            String updateTime = CommonUtil.generateLocalDateTime();
            long updateTimeSort = CommonUtil.generateCurrentTimeMillis();
            gruSysStu.setCreateName(CommonUtil.getUser(session).getUserName());
            gruSysStu.setCreateTime(updateTime);
            gruSysStu.setCreateTimeSort(updateTimeSort);
            gruSysStu.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysStu.setUpdateTime(updateTime);
            gruSysStu.setUpdateTimeSort(updateTimeSort);
            result = gruSysUserService.insertGruSysStu(gruSysStu);
            if (result == null) {
                return ResMessage.error("数据插入失败", 200);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/addStu报错", 500);
        }
        return ResMessage.ok(result);
    }

    /**
     * 更新学生数据
     *
     * @param gruSysStu
     * @return
     */
    @ApiOperation(value = "更新学生数据", notes = "更新学生数据")
    @PostMapping("/editStu")
    public String editStu(@RequestBody GruSysStu gruSysStu, HttpSession session) {
        GruSysStu result = null;
        try {
            if (StringUtils.isEmpty(gruSysStu.getUserId())) {
                return ResMessage.error("更新的数据必须有主键id", 200);
            }
            // 账号不能为空
            String userAccount = gruSysStu.getUserAccount();
            if (StringUtils.isEmpty(userAccount)) {
                return ResMessage.error("账号不能为空", 200);
            }
            // 账号不能重复
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_account", userAccount);
            queryWrapper.eq("del_flag", "0");
            queryWrapper.ne("user_id", gruSysStu.getUserId());
            List<GruSysUser> list = gruSysUserService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                return ResMessage.error("账号不能重复", 200);
            }
            gruSysStu.setUserType(UserType.STUDENT.getValue());
            // 创建人只能取用户账号，不取用户名称了，因为用户名称不是同一个字段，取起来太麻烦
            gruSysStu.setUpdateName(CommonUtil.getUser(session).getUserName());
            gruSysStu.setUpdateTime(CommonUtil.generateLocalDateTime());
            gruSysStu.setUpdateTimeSort(CommonUtil.generateCurrentTimeMillis());
            result = gruSysUserService.updateGruSysStu(gruSysStu);
            if (result == null) {
                return ResMessage.error("数据更新失败", 200);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/editStu报错", 500);
        }
        return ResMessage.ok(result);
    }

    @ApiOperation(value = "按id获取学生数据", notes = "按id获取学生数据")
    @GetMapping("/getStu")
    public String getStu(String userId) {
        GruSysStuVo gruSysStuVo = null;
        try {
            if (StringUtils.isEmpty(userId)) {
                return ResMessage.error("获取数据必须有主键id", 200);
            }
            GruSysUser gruSysUser = gruSysUserService.getById(userId);
            GruSysUser gruSysUser1 = gruSysUserService.getById(gruSysUser.getStuGurdianId());
            GruSysUser gruSysUser2 = gruSysUserService.getById(gruSysUser.getStuFatherId());
            gruSysStuVo = new GruSysStuVo();
            BeanUtils.copyProperties(gruSysUser, gruSysStuVo);
            if (gruSysStuVo == null) {
                return ResMessage.error("获取失败", 200);
            }
            gruSysStuVo.setGuarPhone(gruSysUser1.getGuardianPhone());
            gruSysStuVo.setParentName(gruSysUser2.getUserName());
            gruSysStuVo.setFatherPhone(gruSysUser2.getParentPhone());
            gruSysStuVo.setParentEmail(gruSysUser2.getParentEmail());
            gruSysStuVo.setParentRole(gruSysUser2.getParentRole());
            // 处理附件反显结构
            List<String> filePaths = new ArrayList<>();
            String stuFile1Path = gruSysStuVo.getStuFile1Path();
            String stuFile2Path = gruSysStuVo.getStuFile2Path();
            String stuFile3Path = gruSysStuVo.getStuFile3Path();
            String stuFile4Path = gruSysStuVo.getStuFile4Path();
            String stuFile5Path = gruSysStuVo.getStuFile5Path();
            if (StringUtils.isNotEmpty(stuFile1Path)) {
                filePaths.add(stuFile1Path);
            }
            if (StringUtils.isNotEmpty(stuFile2Path)) {
                filePaths.add(stuFile2Path);
            }
            if (StringUtils.isNotEmpty(stuFile3Path)) {
                filePaths.add(stuFile3Path);
            }
            if (StringUtils.isNotEmpty(stuFile4Path)) {
                filePaths.add(stuFile4Path);
            }
            if (StringUtils.isNotEmpty(stuFile5Path)) {
                filePaths.add(stuFile5Path);
            }
            if (!CollectionUtils.isEmpty(filePaths)) {
                QueryWrapper<GruSysPicture> gruSysPictureQueryWrapper = new QueryWrapper<>();
                gruSysPictureQueryWrapper.in("picture_path", filePaths);
                List<GruSysPicture> list = gruSysPictureService.list(gruSysPictureQueryWrapper);
                if (!CollectionUtils.isEmpty(list)) {
                    for (GruSysPicture gruSysPicture : list) {
                        if (gruSysPicture.getPicturePath().equals(stuFile1Path)) {
                            gruSysStuVo.setGruSysFile1(gruSysPicture);
                        } else if (gruSysPicture.getPicturePath().equals(stuFile2Path)) {
                            gruSysStuVo.setGruSysFile2(gruSysPicture);
                        } else if (gruSysPicture.getPicturePath().equals(stuFile3Path)) {
                            gruSysStuVo.setGruSysFile3(gruSysPicture);
                        } else if (gruSysPicture.getPicturePath().equals(stuFile4Path)) {
                            gruSysStuVo.setGruSysFile4(gruSysPicture);
                        } else if (gruSysPicture.getPicturePath().equals(stuFile5Path)) {
                            gruSysStuVo.setGruSysFile5(gruSysPicture);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/getStu报错", 500);
        }
        return ResMessage.ok(gruSysStuVo);
    }

    @ApiOperation(value = "删除学生数据", notes = "删除学生数据")
    @PostMapping("/delStu")
    public String delStu(@RequestBody GruSysStaff gruSysStaff) {
        try {
            if (StringUtils.isEmpty(gruSysStaff.getUserId())) {
                return ResMessage.error("删除数据必须有主键id", 200);
            }
            gruSysStaff.setDelFlag("1");
            GruSysUser gruSysUser = new GruSysUser();
            BeanUtils.copyProperties(gruSysStaff, gruSysUser);
            // 逻辑删除
            boolean b = gruSysUserService.updateById(gruSysUser);
            if (b) {
                return ResMessage.ok("删除数据成功");
            }
            return ResMessage.error("删除数据失败", 200);
        } catch (Exception e) {
            e.printStackTrace();
            return ResMessage.error("/user/delStu报错", 500);
        }

    }

    // ==============================================学生部分结束


    // 修改密码
    @ApiOperation(value = "修改密码", notes = "修改密码")
    @PostMapping("/editUserPassword")
    public String editUserPassword(@RequestBody GruSysUser gruSysUserParam) {
        GruSysUser gruSysUser = gruSysUserService.getById(gruSysUserParam.getUserId());
        if(!gruSysUser.getUserPassword().equals(gruSysUserParam.getUserPassword())){
            return ResMessage.error("输入正确的密码", 500);
        }

        gruSysUserParam.setUserPassword(gruSysUserParam.getUserPasswordNew());
        boolean resultValue = gruSysUserService.updateById(gruSysUserParam);
        if(!resultValue){
            return ResMessage.error("修改密码失败！", 500);
        }
        return ResMessage.ok("修改密码成功");
    }

    @ApiOperation(value = "家长获取孩子信息", notes = "家长获取孩子信息")
    @PostMapping("/parentGetStudent")
    public String parentGetStudent(HttpSession session) {
        LoginUser user = CommonUtil.getUser(session);
        if (user == null) {
            return ResMessage.error(ExceptionEnums.LOGINUSER_IS_NULL.getEmsg(), ExceptionEnums.LOGINUSER_IS_NULL.getEcode());
        }
        String userType = user.getUserType();
        if (userType.equals(UserType.PARENT.getValue())) {
            QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.and(qw -> qw.like("stu_father_id", user.getUserId())
                    .or().like("stu_mother_id", user.getUserId()));
            List<GruSysUser> list = gruSysUserService.list(queryWrapper);
            return ResMessage.ok(list);
        }
        return ResMessage.error("您的登录信息不是家长", 200);
    }

    @ApiOperation(value = "获取用户信息",notes = "获取用户信息")
    @PostMapping("/getUserInfo")
    public String getUserInfo(@RequestBody GruSysUser gruSysUser){
        String userIdStr = gruSysUser.getUserId();
        GruSysUser gruSysUserResult = gruSysUserService.getById(userIdStr);
        return ResMessage.ok(gruSysUserResult);
    }
}
